package com.lenovo.manufacture;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PersonnelActivity extends AppCompatActivity {

    private ListView perlist;
    private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private ImageView lxSort;
    private ImageView xzSort;
    private List<PersonnelData.DataBean> data=new ArrayList<>();
    MyAdapter myAdapter;
    int k=0;
    int x=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnel);
        initView();
        getPersonnelInfo();
    }

    private void getPersonnelInfo() {
        Network network = retrofit.create(Network.class);
        Call<PersonnelData> personnel = network.getPersonnel();
        personnel.enqueue(new Callback<PersonnelData>() {
            @Override
            public void onResponse(Call<PersonnelData> call, Response<PersonnelData> response) {
                data.addAll(response.body().getData());
                myAdapter=new MyAdapter();
                perlist.setAdapter(myAdapter);
                setEvent();
            }

            @Override
            public void onFailure(Call<PersonnelData> call, Throwable t) {

            }
        });
    }
    private void setEvent(){
        lxSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (k==0){
                    lxSort.setImageResource(R.drawable.up);
                    Collections.sort(data, new Comparator<PersonnelData.DataBean>() {
                        @Override
                        public int compare(PersonnelData.DataBean dataBean, PersonnelData.DataBean t1) {
                            int i=dataBean.getType()-t1.getType();
                            return i;
                        }
                    });
                    k=1;
                    myAdapter.notifyDataSetChanged();
                }else {
                    lxSort.setImageResource(R.drawable.down);
                    Collections.sort(data, new Comparator<PersonnelData.DataBean>() {
                        @Override
                        public int compare(PersonnelData.DataBean dataBean, PersonnelData.DataBean t1) {
                            int i=t1.getType()-dataBean.getType();
                            return i;
                        }
                    });
                    k=0;
                    myAdapter.notifyDataSetChanged();

                }

            }
        });
        xzSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (x==0){
                    xzSort.setImageResource(R.drawable.up);
                    Collections.sort(data, new Comparator<PersonnelData.DataBean>() {
                        @Override
                        public int compare(PersonnelData.DataBean dataBean, PersonnelData.DataBean t1) {
                            int i=dataBean.getPrice()-t1.getPrice();
                            return i;
                        }
                    });
                    x=1;
                    myAdapter.notifyDataSetChanged();
                }else {
                    xzSort.setImageResource(R.drawable.down);
                    Collections.sort(data, new Comparator<PersonnelData.DataBean>() {
                        @Override
                        public int compare(PersonnelData.DataBean dataBean, PersonnelData.DataBean t1) {
                            int i=t1.getPrice()-dataBean.getPrice();
                            return i;
                        }
                    });
                    x=0;
                    myAdapter.notifyDataSetChanged();

                }

            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        private TextView nameText;
        private TextView typeText;
        private TextView wagesText;

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(PersonnelActivity.this).inflate(R.layout.person_row, viewGroup, false);
            nameText = (TextView) view.findViewById(R.id.name_text);
            typeText = (TextView) view.findViewById(R.id.type_text);
            wagesText = (TextView) view.findViewById(R.id.wages_text);
            nameText.setText(data.get(i).getPersonName());
            if (data.get(i).getType()==0){
                typeText.setText("工程师");
            }else if (data.get(i).getType()==1){
                typeText.setText("工人");
            }else if (data.get(i).getType()==2){
                typeText.setText("技术人员");
            }else {
                typeText.setText("质检员");
            }
            wagesText.setText(String.valueOf(data.get(i).getPrice()));

            return view;
        }


    }

    private void initView() {
        perlist = (ListView) findViewById(R.id.perlist);
        lxSort = (ImageView) findViewById(R.id.lx_sort);
        xzSort = (ImageView) findViewById(R.id.xz_sort);
    }
}